package GUINew;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import java.awt.image.BufferedImage;

import javax.swing.SwingConstants;

import com.charliemouse.cambozola.PercentArea;
import com.charliemouse.cambozola.shared.CamStream;
import com.charliemouse.cambozola.shared.ImageChangeEvent;

public class VideoStreamFrame extends JPanel
{
   private JPanel panel;
   
   private Image m_backingStore = null;
   private static final int IMG_TYPE = BufferedImage.TYPE_INT_RGB;
   private JLabel lblVideostream;
   private PercentArea m_area = new PercentArea();
   private CamStream m_imgStream;
   /**
    * Create the frame.
    */
   public VideoStreamFrame(CamStream m_imgStream )
   {
      this.m_imgStream  = m_imgStream;
      setForeground(SystemColor.control);
      setSize(new Dimension(640, 480));
      GridBagLayout gridBagLayout = new GridBagLayout();
      gridBagLayout.columnWidths = new int[]{638, 0};
      gridBagLayout.rowHeights = new int[]{10, 0};
      gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
      gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
      setLayout(gridBagLayout);
      setMaximumSize(new Dimension(640, 480));
      setMinimumSize(new Dimension(640, 480));
      setPreferredSize(new Dimension(640, 480));
     
      panel = new JPanel();
      panel.setPreferredSize(new Dimension(640, 480));
      panel.setMaximumSize(new Dimension(640, 480));
      panel.setMinimumSize(new Dimension(640, 480));
      panel.setBackground(SystemColor.desktop);
      panel.setForeground(Color.BLACK);
      GridBagConstraints gbc_panel = new GridBagConstraints();
      gbc_panel.fill = GridBagConstraints.BOTH;
      gbc_panel.gridx = 0;
      gbc_panel.gridy = 0;
      add(panel, gbc_panel);
      panel.setLayout(new BorderLayout(0, 0));
      
      lblVideostream = new JLabel("VideoStream");
      lblVideostream.setHorizontalAlignment(SwingConstants.CENTER);
      lblVideostream.setForeground(SystemColor.text);
      panel.add(lblVideostream);
   }
   
   public JPanel getPanel()
   {
      return panel;
   }

   public void setPanel(JPanel panel)
   {
      this.panel = panel;
   }
   
   public void paint(Graphics g)
   {
       update(g);
   }

   public void update(Graphics g)
   {
       if (g == null) return;
       Dimension d = getSize();
       if (m_backingStore == null || m_backingStore.getWidth(this) != d.width || m_backingStore.getHeight(this) != d.height) {
           m_backingStore = new BufferedImage(d.width, d.height, IMG_TYPE);
       }
       Graphics gg2 = m_backingStore.getGraphics();
           gg2.setPaintMode();
           //
       if (m_imgStream != null) {
           Image img = m_imgStream.getCurrent();
           if (img != null) {
               paintFrame(gg2, img, d);
           }
       }
       g.drawImage(m_backingStore, 0, 0, null);
       gg2.dispose();
   }


   public void paintFrame(Graphics g, Image img, Dimension d)
   {
       //
       // Draw the main image...
       //
       int indent = 0;
       int m_imgWidth = img.getWidth(this);
       int m_imgHeight = img.getHeight(this);
       if (m_imgWidth == -1 || m_imgHeight == -1) return; // No size for the image, no zoom.
       //
       //
       // Work out the area to zoom into.
       //
       Rectangle imgarea = m_area.getArea(m_imgWidth, m_imgHeight);

       g.drawImage(img, indent, 0, d.width, d.height, imgarea.x, imgarea.y, imgarea.x + imgarea.width, imgarea.y + imgarea.height, this);
       //
       // Draw the accessories...
       //
   }

}
